package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.domain.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVORedisService;
import cn.wolfcode.luowowo.common.util.BeanUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 *
 * 监听器初始化vo数据
 *
 *
 */

//@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {

    @Reference
    private IStrategyDetailService strategyDetailService;


    @Reference
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {


        //获取所有的攻略数据
        List<StrategyDetail> list = strategyDetailService.list();
        //创建vo对象
        StrategyStatisVO vo = null;

        for (StrategyDetail detail : list) {
            vo =  new StrategyStatisVO();
            BeanUtil.copyProperties(vo,detail);

            vo.setDestId(detail.getDest().getId());
            vo.setDestName(detail.getDest().getName());
            //攻略ID
            vo.setStrategyId(detail.getId());

            //同步到redis中
            strategyStatisVORedisService.setStrategyStatisVO(vo);
        }

    }

}
